﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Common;
using Model;

namespace API.Controllers
{
    /// <summary>
    /// 攻略
    /// </summary>
    [RoutePrefix("api/Strateg")]
    public class StrategController : ApiController
    {
        B_Strateg _bs;
        public StrategController()
        {
            _bs = new B_Strateg();
        }

        /// <summary>
        /// 获取所有攻略：分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAll")]
        public IHttpActionResult GetAll(int page,int size)
        {
            var data = from s in _bs.GetAll()
                       join st in new B_StrategType().GetAll()
                       on s.STypeID equals st.STypeID
                       select new Param_Strateg
                       {
                           StrategID = s.StrategID,
                           StrategName = s.StrategName,
                           Paragraph = s.Paragraph,
                           Describe = s.Describe,
                           SImage = s.SImage,
                           Show = s.Show,
                           Thumbsup=s.Thumbsup,
                           CreateTime = s.CreateTime,
                           STypeID = s.STypeID,
                           STypeName = st.STypeName
                       };
            return Ok(new Result<List<Param_Strateg>>
            {
                Data = data.Skip((page-1)*size).Take(size).ToList(),
                Msg=data.Count().ToString()
            });
        }

        /// <summary>
        /// 获取所有攻略
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAll")]
        public IHttpActionResult GetAll()
        { 
            var data = from s in _bs.GetAll()
                       join st in new B_StrategType().GetAll()
                       on s.STypeID equals st.STypeID
                       select new Param_Strateg
                       {
                           StrategID = s.StrategID,
                           StrategName = s.StrategName,
                           Paragraph = s.Paragraph,
                           Describe = s.Describe,
                           SImage = s.SImage,
                           Show = s.Show,
                           Thumbsup = s.Thumbsup,
                           CreateTime = s.CreateTime,
                           STypeID = s.STypeID,
                           STypeName=st.STypeName
                       };
            return Ok(new Result<List<Param_Strateg>>
            {
                Data = data.ToList()
            });
        }
        /// <summary>
        /// 创建攻略
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Add")]
        public IHttpActionResult Add(Param_Strateg param)
        {
            var data = HttpHelper.HttpPost<Result<string>>(Tools.ImgPath, "post", new
            {
                base64 = param.SImage,
                imgurl = "StrategImg",
            });
            if (data.Code == 200)
            {
                try
                {
                    //添加这样写是为了图片
                    var aa = new Strateg
                    {
                        StrategName = param.StrategName,
                        Paragraph = param.Paragraph,
                        Describe = param.Describe,
                        SImage = data.Data,
                        Show = param.Show,
                        Thumbsup = param.Thumbsup,
                        CreateTime = DateTime.Now,
                        STypeID = param.STypeID
                    };
                    _bs.Add(aa);
                    return Ok(new Result<object>());
                }
                catch (Exception)
                {
                    return Ok(new Result<object>
                    {
                        Code = 500,
                        Msg = "出现错误，请联系管理员"
                    });
                }
            }
            return Ok(new Result<object>
            {
                Code = 500,
                Msg = "出现错误，请联系管理员"
            });
        }
        /// <summary>
        /// 删除攻略
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Delete/{id}")]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                _bs.Delete(id);
                return Ok(new Result<object>());
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }
        /// <summary>
        /// 根据编号ID查询攻略
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Find/{id}")]
        public IHttpActionResult Find(int id)
        {
            try
            {
                var data = _bs.FindById(id);
                if (data == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据为空"
                    });
                }
                else
                {
                    return Ok(new Result<Param_Strateg>
                    {
                        Data = new Param_Strateg
                        {
                            StrategID = data.StrategID,
                            StrategName = data.StrategName,
                            Paragraph = data.Paragraph,
                            Describe = data.Describe,
                            SImage = data.SImage,
                            Show = data.Show,
                            Thumbsup = data.Thumbsup,
                            CreateTime = data.CreateTime,
                            STypeID = data.STypeID
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }
        /// <summary>
        /// 根据编号ID和type修改攻略
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Update/{id}")]
        public IHttpActionResult Update(int id, Param_Strateg param)
        {
            try
            {
                var data = _bs.FindById(id);
                if (data == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "攻略数据为空"
                    });
                }
                else
                {
                    var result = HttpHelper.HttpPost<Result<string>>(Tools.ImgPath, "post", new
                    {
                        base64 = param.SImage,
                        imgurl = "StrategImg",
                    });
                    if (result.Code == 200)
                    {
                        _bs.Update(id, new Strateg {
                            StrategName = param.StrategName,
                            Paragraph = param.Paragraph,
                            Describe = param.Describe,
                            SImage = result.Data,
                            Show = param.Show,
                            Thumbsup = param.Thumbsup,
                            CreateTime = DateTime.Now,
                            STypeID = param.STypeID
                        });
                        return Ok(new Result<object>());
                    }
                    return Ok(new Result<object>
                    {
                        Code = 500,
                        Msg = "修改失败"
                    });
                }
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }
    }
}
